<!-- 新增 -->
<template>
  <div class="add-user">
    <el-form 
      :model="user" 
      :rules="rules" 
      ref="userForm" 
      label-width="100px"
      class="user-form"
    >
      <el-form-item label="登录名" prop="loginName">
        <el-input v-model="user.loginName" placeholder="请输入登录名"></el-input>
      </el-form-item>

      <el-form-item label="用户名" prop="empName">
        <el-input v-model="user.empName" placeholder="请输入用户名"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input v-model="user.password" type="password" placeholder="请输入密码"></el-input>
      </el-form-item>

      <el-form-item label="职位" prop="position">
        <el-input v-model="user.position" placeholder="请输入职位"></el-input>
      </el-form-item>

      <el-form-item label="电子邮件" prop="email">
        <el-input v-model="user.email" placeholder="请输入电子邮件"></el-input>
      </el-form-item>

      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input v-model="user.phoneNumber" placeholder="请输入手机号码"></el-input>
      </el-form-item>

      <el-form-item label="工资" prop="salary">
        <el-input v-model.number="user.salary" type="number" placeholder="请输入工资"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { SaveUser } from "@/api/user.js";

export default {
  name: "AddUser",
  data() {
    // 自定义验证手机号
    const validatePhone = (rule, value, callback) => {
      if (value && !/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error('请输入正确的手机号码'));
      } else {
        callback();
      }
    };
    
    return {
      user: {
        loginName: '',
        empName: '',
        password: '',
        position: '',
        email: '',
        phoneNumber: '',
        salary: ''
      },
      rules: {
        loginName: [
          { required: true, message: '请输入登录名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        empName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ],
        position: [
          { required: true, message: '请输入职位', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],
        salary: [
          { required: true, message: '请输入工资', trigger: 'blur' },
          { type: 'number', message: '工资必须为数字', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm() {
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          SaveUser(this.user).then((res) => {
            if (res.status) {
              this.$message.success(res.message);
              this.resetForm();
              // 回退到列表页
              this.$router.replace('/user/index');
            } else {
              this.$message.error(res.message);
            }
          }).catch(error => {
            this.$message.error('保存失败：' + error.message);
          });
        } else {
          this.$message.warning('请正确填写表单！');
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.userForm.resetFields();
    }
  }
};
</script>

<style lang="less" scoped>
.add-user {
  padding: 20px;
  
  .user-form {
    max-width: 600px;
    margin: 0 auto;
  }
}
</style>